package business_logic.game_elements;


import java.util.Collection;
import java.util.EnumSet;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;


import model.collisions.CollisionCategory;
import model.elements.NonPlayerCharacter;
import model.elements.ArtificialIntelligence.MovesRandomShootsRandom;
import model.elements.attributes.Coordinate2D;
import model.elements.attributes.Damage;
import model.elements.attributes.Lives;
import model.elements.attributes.Speed;
import model.elements.configurations.NonPlayerCharacterConfig;
import model.elements.states.weapon.BombEquipped;
import model.exceptions.ModelExceptions;
import business_logic.GameElement;
import business_logic.Tile;
import business_logic.view.GameViewIdentifier;

public class Cecilio extends NonPlayerCharacter implements GameElement
{

    private static final Collection<GameViewIdentifier> m_viewIdentifiers = 
            EnumSet.of(GameViewIdentifier.SPRITE_CECILIO);
    
    public Cecilio(Coordinate2D startingPosition) throws ModelExceptions
    {
        super(startingPosition, new ConfigCecilio());
    }

    public Collection<GameViewIdentifier> getViewIdentifiers()
    {
        return m_viewIdentifiers;
    }

    @Override
    public void vivir()
    {
        try
        {
            this.update();
        }
        catch (ModelExceptions e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public int getX()
    {
        return Tile.pixelToTile( m_currentPosition.getRealX() );
    }

    @Override
    public int getY()
    {
        return Tile.pixelToTile( m_currentPosition.getRealY() );
    }
    
    private static final long m_serialVersionUID = 1L;
    
    @Override
    public Element save()
    {
        Element elem = DocumentHelper.createElement( this.getClass().getName() );
        elem.addAttribute( "serialVersion", m_serialVersionUID+"" );
        elem.addAttribute( "x", Math.floor( this.getPosition().getRealX() )+0.5 +"" );
        elem.addAttribute( "y", Math.floor( this.getPosition().getRealY() )+0.5 +"" );
        
        return elem;
    }
    
    public Cecilio( Element elem ) throws ModelExceptions
    {
        super
        (
            new Coordinate2D
            (
                Double.parseDouble(elem.attributeValue("x")),
                Double.parseDouble(elem.attributeValue("y"))
            ),
            new ConfigCecilio()
        );
    }
}


class ConfigCecilio extends NonPlayerCharacterConfig
{
	private static final int 				CecilioHitPoints 		 = 5;
	private static final double 			CecilioBaseSpeed 		 = 0.003;
	private static final int 				CecilioBaseAmmo 		 = 3;
	private static final int 				CecilioContactDamage 	 = 5;
	private static final CollisionCategory 	CecilioCollisionCategory = CollisionCategory.C_NPC;
	
    public ConfigCecilio( ) {
        m_lives          = new Lives(CecilioHitPoints);
        m_speed          = new Speed(CecilioBaseSpeed);
        m_equippedWeapon = new BombEquipped(CecilioBaseAmmo);
        m_IA             = new MovesRandomShootsRandom( Tile.speedToTickets( CecilioBaseSpeed ) );
        m_damage         = new Damage(CecilioContactDamage);
        m_category       = CecilioCollisionCategory;
    }
}